package com.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import android.content.Intent;
import android.view.View;

public class EventActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_event);
		LinearLayout r = new LinearLayout(this);
		setContentView(r);
		// 设置该Activity显示layout
		r.setOrientation(LinearLayout.VERTICAL);
		
		TextView t = new TextView(this);
		t.setText("this event activity");
		
		Button b = new Button(this);
		b.setText("Return MainActivity");
		b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		b.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v)
			{
				Intent i = new Intent();
				i.setClass(EventActivity.this, MainActivity.class);
				startActivity(i);
				EventActivity.this.finish();
			}
		});
		
		Button b1 = new Button(this);
		b1.setText("TouchEvent");
		b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		b1.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event)
			{
				MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
				mlp.setMargins((int)event.getRawX(), (int)event.getRawY(), 0, 0);
				v.setLayoutParams(mlp);
				return true;
			}
		});
		
		r.addView(t);
		r.addView(b);
		r.addView(b1);
		
		//RelativeLayout布局view会产生重叠，修改button位置，必须放在addView()之后
		/*
		MarginLayoutParams mlp = (MarginLayoutParams) b.getLayoutParams();
		mlp.setMargins(0, 26, 0, 0);
		b.setLayoutParams(mlp);
		*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.event, menu);
		return true;
	}

}
